/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include <string>

#include "SoundBuffer.h"
#include "../shared/geom.h"


/**
======================
The Actual Sound
======================
*/
class SoundSource
{
public:
	SoundSource(void);
	/** Load the source from a file */
	int Load(const std::string &sound_file );
	/** Test to see if we are able to play the sound */
	bool IsReady();
	/** Update the location of the sound */
	void Update(long deltaTime);
	/** Destroy the sound */
	void Destroy();
	/** Checks to see if this sound needs to be
	 *	pruned out
	 */
	bool IsDestroyed() { return m_destroy; };

	/**
	 * Plays the sound, loops if necessary
	 */
	void Play(bool loop);
    /** Loop the sound */
    void Loop();
	/** Stops the sound */
	void Stop();
	/** Pauses the sound */
	void Pause();
	/** Resumes the sound */
	void Resume();

	/** Test the state of the sound */
	bool IsPlaying() { return m_playing; };
	bool IsStopped() { return m_stopped; };
	bool IsPaused()  { return m_paused;  };
	bool Looped()    { return m_looping; };

	/** Sets the volume of the sound */
	void Volume( float v );
	/** Gets the volume */
	float GetVolume() { return m_volume; }

	/** 
	 *  Set the velocity of motion of the sound
	 *  this is for fade effects
	 */
	void SetVelocity(const MVector3f &vel) { m_vel = vel; };
	/** Set the position of the sound */
	void SetPosition(const MVector3f &pos) { m_pos = pos; };

	/** Get velocity */
	MVector3f GetVelocity() { return m_vel; };
	/** Get the posistion */
	MVector3f GetPosition() { return m_pos; };

	//
	// Listener - where the camera is
	//
	
	/** Where the camera lies */
	void SetListenerPos(const MVector3f cam) { m_lpos = cam; };
	/** The speed of the camera */
	void SetListenerVel(const MVector3f vel) { m_lvel = vel; };

    /** Get Size in bytes */
    int  GetSizeInBytes() const;
    /** Get Frequency */
    float  GetFrequency() const;

private:
	SoundBuffer         m_buffer;               // used for alGenBuffers(1, &buffer);
	ALuint              m_source;

	
	bool                m_destroy;              // test to see if this source needs killing
	
	
	bool                m_ready;                // if this sound source is ready

	bool                m_playing;              //
	bool                m_looping;              //  Sound State control
	bool                m_paused;               //
	bool                m_stopped;              //

	float               m_volume;               // volume

	MVector3f           m_pos,                  //  Position of this sound
                        m_vel,                  //  Velocity of this sound ( for dobbler effect )
	                    m_lpos,                 //  The Listeners relative position ( usually the Camera )
                        m_lvel;                 //  The listeners velocity
public:
	virtual ~SoundSource(void);
};
